1749C - Number Game - CodeForces Solution


greedy

Please click on ads to support us..

Python Code:

from collections import defaultdict
first = -1
def possible(arr, n, k):
    d = defaultdict(int)
    for i in arr:
        d[i] += 1
    ans = 0
    if d[1] == 0:
        return 0
    st = 0
    K = k
    while True:
        k = K - st 
        while k > 0 and d[k] == 0:
            k -= 1
        
        if k == 0:
            return 0
        if k==1 and d[k] > 0 and st == K-1:
            return 1
        d[k] -= 1
        
        d[1] -= 1
        st += 1
        if st == K:
            return 1
        if d[1] <= 0:
            return 0
        k -= 1
        
        

for t in range(int(input())):
    n = int(input())
    arr = list(map(int, input().split()))
    
    if t == 0 and first == -1:
        first = n
    ans = 0
    for i in range(n):
        if possible(arr, n, i+1):
            ans = i+1
    
    print(ans)

C++ Code:

#include <bits/stdc++.h>
using namespace std;

#define ctz __builtin_ctz
#define ctzll __builtin_ctzll
#define clz __builtin_clz
#define clzll __builtin_clzll
#define setbits __builtin_popcount
#define setbitsll __builtin_popcountll
#define endl "\n"
#define input(v)                        \
    for (auto i = 0; i < v.size(); i++) \
    {                                   \
        cin >> v[i];                    \
    }
#define output(v)         \
    for (auto k : v)      \
    {                     \
        cout << k << " "; \
    }                     \
    cout << "\n";
typedef long long ll;
typedef vector<char> vc;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> pi;
typedef vector<pair<ll, ll>> vp;

void solve();
ll max(ll a, ll b)
{
    if (a >= b)
        return a;
    else
        return b;
}
ll min(ll a, ll b)
{
    if (a >= b)
        return b;
    else
        return a;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("error.txt", "w", stderr);
    freopen("output.txt", "w", stdout);
#endif

    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }

    cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
    return 0;
}
void solve()
{
    ll n;
    cin >> n;
    vector<ll> arr(n);
    map<ll, ll> mp;
    for (ll i = 0; i < n; i++)
    {
        cin >> arr[i];
        mp[arr[i]]++;
    }
    sort(arr.begin(), arr.end());
    ll ans = 0;
    for (ll i = n; i >= 0; i--)
    {
        map<ll, ll> m = mp;
        ll flag = 0;
        for (ll j = 1; j <= i; j++)
        {
            ll val = i - j + 1;
            auto it = m.upper_bound(val);
            if (it == m.begin())
            {
                flag = 1;
                break;
            }
            it--;
            it->second--;
            if (it->second == 0)
            {
                m.erase(it->first);
            }
            ll x = m.begin()->first;
            m.begin()->second--;
            if (m.begin()->second == 0)
            {
                m.erase(m.begin()->first);
            }
            m[x + val]++;
        }
        if (!flag)
        {
            ans = i;
            break;
        }
    }
    cout << ans << endl;
}
//  ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤


Comments

Submit
0 Comments
More Questions

Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football